import 'package:flutter/material.dart';
import 'package:gold/base/provider_scaffold.dart';
import 'package:gold/http/api_address.dart';
import 'package:gold/route/xfg_nav.dart';
import 'package:gold/ui/my/address/address_viewmodel.dart';
import 'package:gold/utils/res.dart';
import 'package:gold/utils/xfg_color.dart';
import 'package:gold/utils/xfg_size.dart';
import 'package:gold/widgets/common_widgets.dart';
import 'package:gold/widgets/xfg_text.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

/// @description: 地址页面
/// @author tangzhentao
/// @created at 21:21 2024/12/7

class AddressPage extends StatelessWidget {
  /// 是否选择地址
  final bool? isPicker;

  const AddressPage({super.key, this.isPicker});

  @override
  Widget build(BuildContext context) {
    return ProviderScaffold<AddressViewmodel>(
      appBar: (ctx) => xfgAppbar(title: '收货地址'),
        model: AddressViewmodel(ApiAddress.list),
        onInit: (data) => data.firstLoad(),
        builder: (ctx, address) => SmartRefresher(
          controller: address.controller,
          enablePullDown: true,
          enablePullUp: true,
          onRefresh: address.refresh,
          onLoading: address.loadMore,
          child: ListView.builder(
            itemCount: address.list.length,
            itemBuilder: (_, index) {
              final item = address.list[index];
              final isLast = index == ApiAddress.add.length - 1;
              return Container(
                margin: EdgeInsets.fromLTRB(XfgSize().size10, XfgSize().size10, XfgSize().size10, isLast ? XfgSize().size10:0),
                child: Ink(
                  decoration: white6Decoration,
                  child: InkWell(
                    onTap: isPicker == true ? () => XfgNav.pop(item):null,
                    borderRadius: BorderRadius.circular(XfgSize().size6),
                    child: Column(
                      children: [
                        Padding(
                          padding: EdgeInsets.fromLTRB(XfgSize().size12, XfgSize().size16, XfgSize().size36, XfgSize().size18),
                          child: Column(
                            children: [
                              Row(
                                children: [
                                  Expanded(child: XfgText('收货人：${item.name?? ''}', fontSize: XfgSize().text16, color: XfgColor.text1D, bold: FontWeight.bold)),
                                  Expanded(child: XfgText(item.phone?? '', fontSize: XfgSize().text16, color: XfgColor.text1D, bold: FontWeight.bold)),
                                ],
                              ),
                              SizedBox(height: XfgSize().size16),
                              XfgText('${item.address??''}${item.houseNumber}', fontSize: XfgSize().text14, color: XfgColor.text3)
                            ],
                          ),
                        ),
                        Container(height: XfgSize().size0_5, color: XfgColor.colorE),
                        Container(
                          padding: EdgeInsets.symmetric(horizontal: XfgSize().size14),
                          child: Row(
                            children: [
                              _handleButton(Res.imagesRadioRight, Res.imagesRadioRightSelect, '默认地址', Size.square(XfgSize().size20), select: item.isDefault == 1,
                                  onTap: () => address.changeDefault(index)),
                              const Spacer(),
                              _handleButton(Res.imagesAddressModify, Res.imagesAddressModify, '编辑', Size.square(XfgSize().size18), select: false,
                                  onTap: () => address.modify(index)),
                              SizedBox(width: XfgSize().size40),
                              _handleButton(Res.imagesAddressDelete, Res.imagesAddressDelete, '删除', Size(XfgSize().size21, XfgSize().size18), select: false,
                                  onTap: () => address.delete(index))
                            ],
                          ),
                        )
                      ],
                    ),
                  ),
                ),
              );
            },
          ),
        ),
      bottomlessBuilder: (_, viewmodel) => Container(
        alignment: Alignment.center,
        margin: EdgeInsets.only(bottom: XfgSize().bottomBarHeight == 0 ? XfgSize().size18:XfgSize().bottomBarHeight),
        child: TextButton(
          onPressed: viewmodel.newAddress,
          style: xfgButtonStyle(Size(XfgSize().size342, XfgSize().size50), radius: XfgSize().size50),
          child: XfgText('新增地址', fontSize: XfgSize().text20, color: XfgColor.white)
        ),
      ),
    );
  }

  /// 处理按钮
  Widget _handleButton(String image, String imageS, String text, Size size,{bool? select, GestureTapCallback? onTap}) => InkWell(
    onTap: onTap,
    child: Row(
      mainAxisSize: MainAxisSize.max,
      children: [
        Image.asset(select == true ? imageS:image, width: size.width, height: size.height),
        SizedBox(width: XfgSize().size10, height: XfgSize().size48,),
        XfgText(text, fontSize: XfgSize().text14, color: select == true ? XfgColor.theme:XfgColor.text2)
      ],
    ),
  );
}
